package com.activenetwork.smlite.page.event;

import java.lang.reflect.Method;
import java.util.List;


import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.activenetwork.smlite.page.generic.NexusProgressPageObjectListener;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.WaitUtil;


public class EventTag {
	private static Logger logger = LoggerFactory.getLogger(EventTag.class);
	private static By rootBy = By.cssSelector("ul#tagList");
	private static By addFolderBtnBy = By.cssSelector("button#addHandler4Tag");
	private static NexusProgressPageObjectListener nexusProgress = new NexusProgressPageObjectListener();
	private WebDriver driver = EnvContext.webDriverTL.get();
	private WaitUtil waitUtil = new WaitUtil();
	private String tagName;
	private String stringUtilMethodName = "equalsIgnoreCase";

	public EventTag(String name) {
		this.tagName = name;
	}
	
	public EventTag(String name, String stringUtilMethodName) {
		this.tagName = name;
		this.stringUtilMethodName = stringUtilMethodName;
	}
	
	public static EventTag addTag(String name){
		WebDriver driver = EnvContext.webDriverTL.get();
		driver.findElement(addFolderBtnBy).click();
		nexusProgress.waitProgress("EventTagBar");
		WebElement li = driver.findElement(rootBy).findElement(By.xpath(".//li[contains(@class, 'tag-item')][last()]"));
		WebElement liText = li.findElement(By.xpath(".//input[@type='text']"));
		liText.clear();
		liText.sendKeys(name, Keys.ENTER);
		nexusProgress.waitProgress("EventTagBar");
		logger.debug("Create tag " + name);
		return new EventTag(name);
	}

	public void delete() {
		WebElement li = getTagElement();
		clickEdit(li);
		try{
			driver.findElement(By.cssSelector("li.tag-menu[data-tag-action='remove'] a")).click();
			nexusProgress.waitProgress("EventTagBar");
			logger.debug("Delete tag " + tagName);
		}catch(NoSuchElementException e){
			throw new RuntimeException("Unable to delete the tag " + tagName);
		}
	}
	
	public EventTag rename(String name){
		WebElement li = getTagElement();
		clickEdit(li);
		try{
			driver.findElement(By.cssSelector("li.tag-menu[data-tag-action='rename'] a")).click();
			waitUtil.wait(250);
			WebElement text = li.findElement(By.xpath(".//input[@type='text']"));
			text.clear();
			text.sendKeys(name, Keys.ENTER);
			nexusProgress.waitProgress("EventTagBar");
			logger.debug("Rename tag " + this.tagName + " to " + name);
			return new EventTag(name);
		}catch(NoSuchElementException e){
			throw new RuntimeException("Unable to rename the tag " + tagName);
		}
	}
	
	
	
	public void highlight(){
		WebElement li = getTagElement();
		li.findElement(By.xpath(".//span[@class='tag-title']")).click();
		logger.debug("Highlight tag " + tagName);
		nexusProgress.waitProgress("EventTagBar");
	}

	private WebElement getTagElement() {
		clickMore();
		List<WebElement> lis = driver.findElement(rootBy).findElements(By.xpath("./li"));
		for(WebElement li : lis){
			String name = li.findElement(By.xpath(".//span[@class='tag-title']")).getText().trim();
			boolean matched = false;
			try{
				matched = match(name, tagName);
			}catch(Exception e){
				
			}
			if(matched){
				return li;
			}

		}
		
		throw new RuntimeException("Not found tag " + tagName);

	}
	
	private boolean match(String str1, String str2) throws Exception{
		Class<?> clazz = Class.forName("org.apache.commons.lang.StringUtils");
		StringUtils util = (StringUtils) clazz.newInstance();
		Method m = clazz.getMethod(this.stringUtilMethodName, String.class, String.class);
		return (Boolean)m.invoke(util, str1, str2);
	}

	private void clickMore() {
		WebElement moreBtn = driver.findElement(By.cssSelector("#loadHandler4Tag"));
		if(!moreBtn.getAttribute("style").contains("display: none")){
			moreBtn.click();
			waitUtil.wait(2000);
		}
	}
	
	private void clickEdit(WebElement li){
		li.findElement(By.xpath("./div/span[contains(@class,'tag-action')]/i")).click();
		waitUtil.wait(250);
	}
}
